package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.ProductDetailDao;
import cn.gson.springboot.model.dao.PurchaseDao;
import cn.gson.springboot.model.mapper.PurchaseMapper;
import cn.gson.springboot.model.pojos.ProductDetail;
import cn.gson.springboot.model.pojos.Purchase;
import cn.gson.springboot.model.vo.ProductDetailVo;
import cn.gson.springboot.model.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PurchaseService {
    @Autowired
    PurchaseMapper purMapper;
    @Autowired
    PurchaseDao purDao;
    @Autowired
    ProductDetailDao proDao;

    public List<Purchase> selectPurchase(Integer value,Integer num,String purTheme){
        return purMapper.selectPurchase(value,num,purTheme);
    }

    public List<Purchase> selectCusPurchase(Integer cusId){
        return purMapper.selectCusPurchase(cusId);
    }

    public Purchase selectPurchaseById(Integer purId){
        return purMapper.selectPurchaseById(purId);
    }

    public Purchase selectPurchaseByPurNo(String purNo){
        return purMapper.selectPurchaseByPurNo(purNo);
    }

    public  List<Purchase> selectPurchaseImplement(){
        return purMapper.selectPurchaseImplement();
    }

    public  List<Purchase> selectPurchasePastThirty(){
        return purMapper.selectPurchasePastThirty();
    }

    public  List<Purchase> selectPurchaseNotPayment(){
        return purMapper.selectPurchaseNotPayment();
    }

    public List<Map<String,Object>> computePurchaseNumBySort(){
        return purMapper.computePurchaseNumBySort();
    }
    public List<Map<String,Object>> computePurchaseNumBySigningByCus(){
        return purMapper.computePurchaseNumBySigningByCus();
    }

    public List<Map<String,Object>> computePurchaseNumByMonth(){
        return purMapper.computePurchaseNumByMonth();
    }

    public List<Map<String,Object>> computePurchaseAmountByMonth(){
        return purMapper.computePurchaseAmountByMonth();
    }

    public List<Map<String,Object>> computeProdDetailAmountByPro(){
        return purMapper.computeProdDetailAmountByPro();
    }
    public List<Map<String,Object>> computeProdDetailNumByPro(){
        return purMapper.computeProdDetailNumByPro();
    }
    public List<Map<String,Object>> computePayPlanNumByPur(){
        return purMapper.computePayPlanNumByPur();
    }
    public List<Map<String,Object>> computePayPlanAmountByPur(){
        return purMapper.computePayPlanAmountByPur();
    }
    public List<Map<String,Object>> computePayPlanAmountStaByCus(){
        return purMapper.computePayPlanAmountStaByCus();
    }
    public List<Map<String,Object>> computePayPlanNumByEmp(){
        return purMapper.computePayPlanNumByEmp();
    }

    public List<Map<String,Object>> computePayPlanAmountByEmp(){
        return purMapper.computePayPlanAmountByEmp();
    }

    public List<Map<String,Object>> computePayPlanAmountByMonth(){
        return purMapper.computePayPlanAmountByMonth();
    }

    public List<Map<String,Object>> computePayRecordsAmountByPur(){
        return purMapper.computePayRecordsAmountByPur();
    }

    public List<Map<String,Object>> computePayRecordsAmountByEmp(){
        return purMapper.computePayRecordsAmountByEmp();
    }

    public List<Map<String,Object>> computePayRecordsAmountByMonth(){
        return purMapper.computePayRecordsAmountByMonth();
    }

    public List<Map<String,Object>> computePayRecordsAmountByClassify(){
        return purMapper.computePayRecordsAmountByClassify();
    }

    public List<Map<String,Object>> computePayInvoiceAmountByPur(){
        return purMapper.computePayInvoiceAmountByPur();
    }

    public List<Map<String,Object>> computePayInvoiceAmountByEmp(){
        return purMapper.computePayInvoiceAmountByEmp();
    }

    public List<Map<String,Object>> computePayInvoiceAmountByMonth(){
        return purMapper.computePayInvoiceAmountByMonth();
    }

    public List<Map<String,Object>> computePayInvoiceAmountByPaperType(){
        return purMapper.computePayInvoiceAmountByPaperType();
    };

    @Transactional(transactionManager = "transactionManager")
    public void savePurchase(PurchaseVo vo){
        Purchase pu = new Purchase();
        pu.setPurchaseId(vo.getPurchaseId());
        pu.setPurchaseTheme(vo.getPurchaseTheme());
        pu.setPurchaseNo(vo.getPurchaseNo());
        pu.setPurchaseDate(vo.getPurchaseDate());
        pu.setPurchasePredictDate(vo.getPurchasePredictDate());
        pu.setPurchaseStatus(vo.getPurchaseStatus());
        pu.setPurchaseSort(vo.getPurchaseSort());
        pu.setPurchaseAmount(vo.getPurchaseAmount());
        pu.setPurchaseNote(vo.getPurchaseNote());
        pu.setPurchaseContactName(vo.getPurchaseContactName());
        pu.setPurchaseBill(vo.getPurchaseBill());
        pu.setPurchasePaid(vo.getPurchasePaid());
        pu.setPurchaseAppStatus(vo.getPurchaseAppStatus());
        pu.setPurchaseReturn(vo.getPurchaseReturn());
        pu.setCustomerByCustomerId(vo.getCustomerByCustomerId());
        pu.setEmployeeByEmpId(vo.getEmployeeByEmpId());
        pu.setCustomerByConCustomerId(vo.getCustomerByConCustomerId());
        pu.setContractByContractId(vo.getContractByContractId());
        pu.setEmployeeByAppEmpId(vo.getEmployeeByAppEmpId());
        pu.setWarehouseByWarehouseId(vo.getWarehouseByWarehouseId());
        purDao.save(pu);

        List<ProductDetailVo> details= vo.getProductDetailsByPurchaseId();
        proDao.deleteByPurId(pu.getPurchaseId());
        for (ProductDetailVo d:details) {
            ProductDetail pd = new ProductDetail();
            pd.setProductDetailId(0);
            pd.setProductDetailRemarks(d.getProductDetailRemarks());
            pd.setProductDetailName(d.getProductDetailName());
            pd.setProductDetailModel(d.getProductDetailModel());
            pd.setProductDetailCount(d.getProductDetailCount());
            pd.setProductDetailPrice(d.getProductDetailPrice());
            pd.setProductDetailMoney(d.getProductDetailMoney());
            pd.setProductDetailTotalMoney(d.getProductDetailTotalMoney());
            pd.setProductDetailIntoNum(d.getProductDetailIntoNum());
            pd.setProductDetailOutNum(d.getProductDetailOutNum());
            pd.setProductByProductId(d.getProductByProductId());
            pd.setPurchaseByPurchaseId(pu);
            proDao.save(pd);
        }
    }

    @Transactional(transactionManager = "transactionManager")
    public void deletePurchase(Integer purchaseId){
        purDao.deleteById(purchaseId);
    }
}
